﻿using System;
using UnityEngine;

// Token: 0x02000043 RID: 67
public static class MMD4MecanimAnim
{
	// Token: 0x06000184 RID: 388 RVA: 0x0000F1A4 File Offset: 0x0000D3A4
	public static void InitializeAnimModel(MMD4MecanimAnim.IAnimModel animModel)
	{
		if (animModel == null)
		{
			return;
		}
		int animCount = animModel.animCount;
		for (int i = 0; i < animCount; i++)
		{
			MMD4MecanimAnim.IAnim animAt = animModel.GetAnimAt(i);
			if (animAt != null)
			{
				if (animAt.animatorStateName != null)
				{
					animAt.animatorStateNameHash = Animator.StringToHash(animAt.animatorStateName);
				}
				if (animAt.animData == null)
				{
					animAt.animData = MMD4MecanimData.BuildAnimData(animAt.animFile);
					if (animAt.animData == null)
					{
						goto IL_1BE;
					}
				}
				MMD4MecanimData.MorphMotionData[] morphMotionDataList = animAt.animData.morphMotionDataList;
				if (morphMotionDataList != null)
				{
					animAt.morphMotionList = new MMD4MecanimAnim.MorphMotion[morphMotionDataList.Length];
					if (animAt.animData.supportNameIsFull)
					{
						for (int num = 0; num != morphMotionDataList.Length; num++)
						{
							animAt.morphMotionList[num].morph = animModel.GetMorph(morphMotionDataList[num].name, morphMotionDataList[num].nameIsFull);
						}
					}
					else
					{
						for (int num2 = 0; num2 != morphMotionDataList.Length; num2++)
						{
							animAt.morphMotionList[num2].morph = animModel.GetMorph(morphMotionDataList[num2].name, false);
						}
						for (int num3 = 0; num3 != morphMotionDataList.Length; num3++)
						{
							if (animAt.morphMotionList[num3].morph == null)
							{
								MMD4MecanimAnim.IMorph morph = animModel.GetMorph(morphMotionDataList[num3].name, true);
								if (morph != null)
								{
									bool flag = false;
									int num4 = 0;
									while (num4 != morphMotionDataList.Length && !flag)
									{
										flag = (animAt.morphMotionList[num4].morph == morph);
										num4++;
									}
									if (!flag)
									{
										animAt.morphMotionList[num3].morph = morph;
									}
								}
							}
						}
					}
				}
			}
			IL_1BE:;
		}
	}

	// Token: 0x06000185 RID: 389 RVA: 0x0000F37A File Offset: 0x0000D57A
	public static void PreUpdateAnimModel(MMD4MecanimAnim.IAnimModel animModel)
	{
		if (animModel == null)
		{
			return;
		}
		if (animModel.prevDeltaTime == 0f)
		{
			animModel.prevDeltaTime = Time.deltaTime;
		}
	}

	// Token: 0x06000186 RID: 390 RVA: 0x0000F39E File Offset: 0x0000D59E
	public static void PostUpdateAnimModel(MMD4MecanimAnim.IAnimModel animModel)
	{
		if (animModel == null)
		{
			return;
		}
		animModel.prevDeltaTime = Time.deltaTime;
	}

	// Token: 0x06000187 RID: 391 RVA: 0x0000F3B4 File Offset: 0x0000D5B4
	public static void UpdateAnimModel(MMD4MecanimAnim.IAnimModel animModel)
	{
		if (animModel == null)
		{
			return;
		}
		Animator animator = animModel.animator;
		if (animator != null && animator.enabled)
		{
			AnimatorStateInfo currentAnimatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
			int fullPathHash = currentAnimatorStateInfo.fullPathHash;
			float animationTime = currentAnimatorStateInfo.normalizedTime * currentAnimatorStateInfo.length;
			int animCount = animModel.animCount;
			for (int i = 0; i < animCount; i++)
			{
				MMD4MecanimAnim.IAnim animAt = animModel.GetAnimAt(i);
				if (animAt != null)
				{
					if (animAt.animatorStateNameHash == fullPathHash)
					{
						MMD4MecanimAnim.UpdateAnimModel(animModel, animAt, animationTime);
						return;
					}
				}
			}
			MMD4MecanimAnim.StopAnimModel(animModel);
		}
		else
		{
			MMD4MecanimAnim.StopAnimModel(animModel);
		}
	}

	// Token: 0x06000188 RID: 392 RVA: 0x0000F464 File Offset: 0x0000D664
	public static void UpdateAnimModel(MMD4MecanimAnim.IAnimModel animModel, MMD4MecanimAnim.IAnim anim, float animationTime)
	{
		if (animModel == null)
		{
			return;
		}
		if (anim == null)
		{
			MMD4MecanimAnim.StopAnimModel(animModel);
			return;
		}
		float num = animationTime * 30f;
		int num2 = (int)num;
		bool flag = animModel.playingAnim != anim;
		if (animModel.playingAnim != anim)
		{
			MMD4MecanimAnim.StopAnimModel(animModel);
		}
		bool isPlayAudioSourceAtFirst = false;
		if (animModel.playingAnim == null && anim.audioClip != null)
		{
			AudioSource audioSource = animModel.audioSource;
			if (audioSource != null)
			{
				if (audioSource.clip != anim.audioClip)
				{
					audioSource.clip = anim.audioClip;
					audioSource.Play();
					isPlayAudioSourceAtFirst = true;
				}
				else if (!audioSource.isPlaying)
				{
					audioSource.Play();
					isPlayAudioSourceAtFirst = true;
				}
			}
		}
		animModel.playingAnim = anim;
		MMD4MecanimAnim._SyncToAudio(animModel, animationTime, isPlayAudioSourceAtFirst);
		MMD4MecanimAnim.MorphMotion[] morphMotionList = anim.morphMotionList;
		MMD4MecanimData.AnimData animData = anim.animData;
		if (morphMotionList != null && animData != null && animData.morphMotionDataList != null)
		{
			for (int num3 = 0; num3 != morphMotionList.Length; num3++)
			{
				MMD4MecanimData.MorphMotionData morphMotionData = animData.morphMotionDataList[num3];
				if (morphMotionList[num3].morph != null)
				{
					if (morphMotionData.frameNos != null && morphMotionData.f_frameNos != null && morphMotionData.weights != null)
					{
						if (flag || morphMotionList[num3].lastKeyFrameIndex >= morphMotionData.frameNos.Length || morphMotionData.frameNos[morphMotionList[num3].lastKeyFrameIndex] > num2)
						{
							morphMotionList[num3].lastKeyFrameIndex = 0;
						}
						bool flag2 = false;
						for (int num4 = morphMotionList[num3].lastKeyFrameIndex; num4 != morphMotionData.frameNos.Length; num4++)
						{
							int num5 = morphMotionData.frameNos[num4];
							if (num2 < num5)
							{
								if (morphMotionList[num3].lastKeyFrameIndex + 1 < morphMotionData.frameNos.Length)
								{
									MMD4MecanimAnim._ProcessKeyFrame2(animModel, morphMotionList[num3].morph, morphMotionData, morphMotionList[num3].lastKeyFrameIndex, morphMotionList[num3].lastKeyFrameIndex + 1, num2, num);
									flag2 = true;
								}
								break;
							}
							morphMotionList[num3].lastKeyFrameIndex = num4;
						}
						if (!flag2 && morphMotionList[num3].lastKeyFrameIndex < morphMotionData.frameNos.Length)
						{
							MMD4MecanimAnim._ProcessKeyFrame(animModel, morphMotionList[num3].morph, morphMotionData, morphMotionList[num3].lastKeyFrameIndex);
						}
					}
				}
			}
		}
	}

	// Token: 0x06000189 RID: 393 RVA: 0x0000F710 File Offset: 0x0000D910
	private static void _ProcessKeyFrame2(MMD4MecanimAnim.IAnimModel animModel, MMD4MecanimAnim.IMorph morph, MMD4MecanimData.MorphMotionData motionMorphData, int keyFrameIndex0, int keyFrameIndex1, int frameNo, float f_frameNo)
	{
		int num = motionMorphData.frameNos[keyFrameIndex0];
		int num2 = motionMorphData.frameNos[keyFrameIndex1];
		float num3 = motionMorphData.f_frameNos[keyFrameIndex0];
		float num4 = motionMorphData.f_frameNos[keyFrameIndex1];
		if (frameNo <= num || num2 - num == 1)
		{
			animModel._SetAnimMorphWeight(morph, motionMorphData.weights[keyFrameIndex0]);
		}
		else if (frameNo >= num2)
		{
			animModel._SetAnimMorphWeight(morph, motionMorphData.weights[keyFrameIndex1]);
		}
		else
		{
			float num5 = (f_frameNo - num3) / (num4 - num3);
			float num6 = motionMorphData.weights[keyFrameIndex0];
			float num7 = motionMorphData.weights[keyFrameIndex1];
			animModel._SetAnimMorphWeight(morph, num6 + (num7 - num6) * num5);
		}
	}

	// Token: 0x0600018A RID: 394 RVA: 0x0000F7B6 File Offset: 0x0000D9B6
	private static void _ProcessKeyFrame(MMD4MecanimAnim.IAnimModel animModel, MMD4MecanimAnim.IMorph morph, MMD4MecanimData.MorphMotionData motionMorphData, int keyFrameIndex)
	{
		animModel._SetAnimMorphWeight(morph, motionMorphData.weights[keyFrameIndex]);
	}

	// Token: 0x0600018B RID: 395 RVA: 0x0000F7C8 File Offset: 0x0000D9C8
	public static void StopAnimModel(MMD4MecanimAnim.IAnimModel animModel)
	{
		if (animModel == null)
		{
			return;
		}
		MMD4MecanimAnim.IAnim playingAnim = animModel.playingAnim;
		if (playingAnim == null)
		{
			return;
		}
		if (playingAnim.audioClip != null)
		{
			AudioSource audioSource = animModel.audioSource;
			if (audioSource != null && audioSource.clip == playingAnim.audioClip)
			{
				audioSource.Stop();
				audioSource.clip = null;
				if (animModel.animSyncToAudio)
				{
					Animator animator = animModel.animator;
					if (animator != null)
					{
						animator.speed = 1f;
					}
				}
			}
		}
		animModel.playingAnim = null;
	}

	// Token: 0x0600018C RID: 396 RVA: 0x0000F864 File Offset: 0x0000DA64
	private static void _SyncToAudio(MMD4MecanimAnim.IAnimModel animModel, float animationTime, bool isPlayAudioSourceAtFirst)
	{
		if (!animModel.animSyncToAudio)
		{
			return;
		}
		MMD4MecanimAnim.IAnim playingAnim = animModel.playingAnim;
		if (playingAnim == null || playingAnim.audioClip == null)
		{
			return;
		}
		AudioSource audioSource = animModel.audioSource;
		if (audioSource == null)
		{
			return;
		}
		Animator animator = animModel.animator;
		if (animator != null && animator.enabled)
		{
			float prevDeltaTime = animModel.prevDeltaTime;
			if (audioSource.isPlaying)
			{
				float time = audioSource.time;
				if (time == 0f)
				{
					animator.speed = 0f;
				}
				else
				{
					float num = (prevDeltaTime + Time.deltaTime) * 0.5f;
					float num2 = time - animationTime;
					if (Mathf.Abs(num2) <= num)
					{
						animator.speed = 1f;
					}
					else if (!isPlayAudioSourceAtFirst && num > Mathf.Epsilon && num < 0.1f)
					{
						float num3 = 1f + num2 / num;
						num3 = Mathf.Clamp(num3, 0.5f, 2f);
						if (animator.speed == 0f)
						{
							animator.speed = num3;
						}
						else
						{
							animator.speed = animator.speed * 0.95f + num3 * 0.05f;
						}
					}
					else
					{
						AnimatorStateInfo currentAnimatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
						if (currentAnimatorStateInfo.length > Mathf.Epsilon)
						{
							int fullPathHash = currentAnimatorStateInfo.fullPathHash;
							animator.Play(fullPathHash, 0, time / currentAnimatorStateInfo.length);
							animator.speed = 1f;
						}
					}
				}
			}
			else
			{
				animator.speed = 1f;
			}
		}
	}

	// Token: 0x02000044 RID: 68
	[Serializable]
	public struct MorphMotion
	{
		// Token: 0x0400026C RID: 620
		public MMD4MecanimAnim.IMorph morph;

		// Token: 0x0400026D RID: 621
		public int lastKeyFrameIndex;
	}

	// Token: 0x02000045 RID: 69
	public interface IAnim
	{
		// Token: 0x17000037 RID: 55
		// (get) Token: 0x0600018D RID: 397
		// (set) Token: 0x0600018E RID: 398
		string animatorStateName { get; set; }

		// Token: 0x17000038 RID: 56
		// (get) Token: 0x0600018F RID: 399
		// (set) Token: 0x06000190 RID: 400
		int animatorStateNameHash { get; set; }

		// Token: 0x17000039 RID: 57
		// (get) Token: 0x06000191 RID: 401
		// (set) Token: 0x06000192 RID: 402
		TextAsset animFile { get; set; }

		// Token: 0x1700003A RID: 58
		// (get) Token: 0x06000193 RID: 403
		// (set) Token: 0x06000194 RID: 404
		AudioClip audioClip { get; set; }

		// Token: 0x1700003B RID: 59
		// (get) Token: 0x06000195 RID: 405
		// (set) Token: 0x06000196 RID: 406
		MMD4MecanimData.AnimData animData { get; set; }

		// Token: 0x1700003C RID: 60
		// (get) Token: 0x06000197 RID: 407
		// (set) Token: 0x06000198 RID: 408
		MMD4MecanimAnim.MorphMotion[] morphMotionList { get; set; }
	}

	// Token: 0x02000046 RID: 70
	[Serializable]
	public class Anim : MMD4MecanimAnim.IAnim
	{
		// Token: 0x1700003D RID: 61
		// (get) Token: 0x0600019A RID: 410 RVA: 0x0000FA03 File Offset: 0x0000DC03
		// (set) Token: 0x0600019B RID: 411 RVA: 0x0000FA0B File Offset: 0x0000DC0B
		string MMD4MecanimAnim.IAnim.animatorStateName
		{
			get
			{
				return this.animatorStateName;
			}
			set
			{
				this.animatorStateName = value;
			}
		}

		// Token: 0x1700003E RID: 62
		// (get) Token: 0x0600019C RID: 412 RVA: 0x0000FA14 File Offset: 0x0000DC14
		// (set) Token: 0x0600019D RID: 413 RVA: 0x0000FA1C File Offset: 0x0000DC1C
		int MMD4MecanimAnim.IAnim.animatorStateNameHash
		{
			get
			{
				return this.animatorStateNameHash;
			}
			set
			{
				this.animatorStateNameHash = value;
			}
		}

		// Token: 0x1700003F RID: 63
		// (get) Token: 0x0600019E RID: 414 RVA: 0x0000FA25 File Offset: 0x0000DC25
		// (set) Token: 0x0600019F RID: 415 RVA: 0x0000FA2D File Offset: 0x0000DC2D
		TextAsset MMD4MecanimAnim.IAnim.animFile
		{
			get
			{
				return this.animFile;
			}
			set
			{
				this.animFile = value;
			}
		}

		// Token: 0x17000040 RID: 64
		// (get) Token: 0x060001A0 RID: 416 RVA: 0x0000FA36 File Offset: 0x0000DC36
		// (set) Token: 0x060001A1 RID: 417 RVA: 0x0000FA3E File Offset: 0x0000DC3E
		AudioClip MMD4MecanimAnim.IAnim.audioClip
		{
			get
			{
				return this.audioClip;
			}
			set
			{
				this.audioClip = value;
			}
		}

		// Token: 0x17000041 RID: 65
		// (get) Token: 0x060001A2 RID: 418 RVA: 0x0000FA47 File Offset: 0x0000DC47
		// (set) Token: 0x060001A3 RID: 419 RVA: 0x0000FA4F File Offset: 0x0000DC4F
		MMD4MecanimData.AnimData MMD4MecanimAnim.IAnim.animData
		{
			get
			{
				return this.animData;
			}
			set
			{
				this.animData = value;
			}
		}

		// Token: 0x17000042 RID: 66
		// (get) Token: 0x060001A4 RID: 420 RVA: 0x0000FA58 File Offset: 0x0000DC58
		// (set) Token: 0x060001A5 RID: 421 RVA: 0x0000FA60 File Offset: 0x0000DC60
		MMD4MecanimAnim.MorphMotion[] MMD4MecanimAnim.IAnim.morphMotionList
		{
			get
			{
				return this.morphMotionList;
			}
			set
			{
				this.morphMotionList = value;
			}
		}

		// Token: 0x0400026E RID: 622
		public string animatorStateName;

		// Token: 0x0400026F RID: 623
		[NonSerialized]
		public int animatorStateNameHash;

		// Token: 0x04000270 RID: 624
		public TextAsset animFile;

		// Token: 0x04000271 RID: 625
		public AudioClip audioClip;

		// Token: 0x04000272 RID: 626
		[NonSerialized]
		public MMD4MecanimData.AnimData animData;

		// Token: 0x04000273 RID: 627
		[NonSerialized]
		public MMD4MecanimAnim.MorphMotion[] morphMotionList;
	}

	// Token: 0x02000047 RID: 71
	public interface IMorph
	{
		// Token: 0x17000043 RID: 67
		// (get) Token: 0x060001A6 RID: 422
		string name { get; }

		// Token: 0x17000044 RID: 68
		// (get) Token: 0x060001A7 RID: 423
		// (set) Token: 0x060001A8 RID: 424
		float weight { get; set; }
	}

	// Token: 0x02000048 RID: 72
	public interface IAnimModel
	{
		// Token: 0x060001A9 RID: 425
		MMD4MecanimAnim.IMorph GetMorph(string name);

		// Token: 0x060001AA RID: 426
		MMD4MecanimAnim.IMorph GetMorph(string name, bool startsWith);

		// Token: 0x17000045 RID: 69
		// (get) Token: 0x060001AB RID: 427
		int morphCount { get; }

		// Token: 0x060001AC RID: 428
		MMD4MecanimAnim.IMorph GetMorphAt(int index);

		// Token: 0x17000046 RID: 70
		// (get) Token: 0x060001AD RID: 429
		int animCount { get; }

		// Token: 0x060001AE RID: 430
		MMD4MecanimAnim.IAnim GetAnimAt(int index);

		// Token: 0x17000047 RID: 71
		// (get) Token: 0x060001AF RID: 431
		Animator animator { get; }

		// Token: 0x17000048 RID: 72
		// (get) Token: 0x060001B0 RID: 432
		AudioSource audioSource { get; }

		// Token: 0x17000049 RID: 73
		// (get) Token: 0x060001B1 RID: 433
		bool animSyncToAudio { get; }

		// Token: 0x1700004A RID: 74
		// (get) Token: 0x060001B2 RID: 434
		// (set) Token: 0x060001B3 RID: 435
		float prevDeltaTime { get; set; }

		// Token: 0x1700004B RID: 75
		// (get) Token: 0x060001B4 RID: 436
		// (set) Token: 0x060001B5 RID: 437
		MMD4MecanimAnim.IAnim playingAnim { get; set; }

		// Token: 0x060001B6 RID: 438
		void _SetAnimMorphWeight(MMD4MecanimAnim.IMorph morph, float weight);
	}
}
